#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkLight.h>
#include <vtkLightActor.h>
#include <vtkLightCollection.h>
#include <vtkNamedColors.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <cstdlib>

int main(int argc, char **argv)
{
    vtkNew<vtkNamedColors> colors;

    vtkNew<vtkRenderer> renderer;
    renderer->SetBackground(colors->GetColor3d("black").GetData());

    double lightPosition[3] = {0, 0, 1};

    // 创建一个光源
    double lightFocalPoint[3] = {0, 0, 0};
    // 光源部分设置
    vtkNew<vtkLight> light;
    light->SetLightTypeToSceneLight();
    light->SetColor(0,1,0);
    light->SetPosition(lightPosition[0], lightPosition[1], lightPosition[2]);
    //表明光源是一个位置光源
    light->SetPositional(true);
    light->SetConeAngle(10);
    //设置焦点位置
    light->SetFocalPoint(lightFocalPoint[0], lightFocalPoint[1], lightFocalPoint[2]);
    //漫反射颜色
    light->SetDiffuseColor(colors->GetColor3d("Red").GetData());
    //环境颜色
    light->SetAmbientColor(colors->GetColor3d("Green").GetData());
    //镜面颜色
    light->SetSpecularColor(colors->GetColor3d("Blue").GetData());

    vtkLightCollection *originalLights = renderer->GetLights();
    std::cout << "Originally there are " << originalLights->GetNumberOfItems()
              << " lights." << std::endl;

    // 显示  光源在哪
    vtkNew<vtkLightActor> lightActor;
    lightActor->SetLight(light);
    renderer->AddViewProp(lightActor);
    // 显示光源的关注点在哪
    //创建一个球
    vtkNew<vtkSphereSource> lightFocalPointSphere;
    lightFocalPointSphere->SetCenter(lightFocalPoint);
    lightFocalPointSphere->SetRadius(0.1);
    lightFocalPointSphere->Update();


    vtkNew<vtkPolyDataMapper> lightFocalPointMapper;
    lightFocalPointMapper->SetInputConnection(lightFocalPointSphere->GetOutputPort());

    vtkNew<vtkActor> lightFocalActor;
    lightFocalActor->SetMapper(lightFocalPointMapper);
    lightFocalActor->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());
    renderer->AddViewProp(lightFocalActor);

    // 创建一个平面
    vtkNew<vtkPlaneSource> planeSource;
    planeSource->SetResolution(100, 100);
    planeSource->Update();

    vtkNew<vtkPolyDataMapper> planeMapper;
    planeMapper->SetInputData(planeSource->GetOutput());

    vtkNew<vtkActor> planeActor;
    planeActor->SetMapper(planeMapper);
    renderer->AddActor(planeActor);


    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(600,600);
    renderWindow->SetWindowName("light");

    vtkNew<vtkRenderWindowInteractor> iren;
    iren->SetRenderWindow(renderWindow);

    renderWindow->Render();
    std::cout << "Now there are " << originalLights->GetNumberOfItems()
              << " lights." << std::endl;

    renderer->AddLight(light);
    std::cout << "Now there are " << originalLights->GetNumberOfItems()
              << " lights." << std::endl;
    auto camera = renderer->GetActiveCamera();
    camera->SetPosition(-2.17199, -2.50774, 2.18);
    camera->SetFocalPoint(-0.144661, -0.146372, 0.180482);
    camera->SetViewUp(0.0157883, 0.638203, 0.769706);
    camera->SetDistance(3.69921);
    camera->SetClippingRange(1.76133, 6.14753);

    iren->Start();

    return EXIT_SUCCESS;
}